package com.android.svmc.app.mobile.data;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.android.svmc.app.mobile.model.CommentModel;
import com.android.svmc.app.mobile.model.MenuItem;
import com.android.svmc.app.mobile.model.PictureModel;
import com.android.svmc.app.mobile.model.PostDetail;
import com.android.svmc.app.mobile.model.PostDetailOffline;
import com.android.svmc.app.mobile.util.CommonDefine;

public class JsonData implements CommonDefine{
	Context mcontext;
	
	public JsonData(Context mcontext) {
		super();
		this.mcontext = mcontext;
	}
	
	public JsonData() {
		super();
	}
	/**
	 *  get list Category online or offline
	 *  @param : String data : = "" if online
	 * @return
	 */
	public ArrayList<MenuItem> getListCategory (String data ,boolean isonline){
		JSONArray categories;
		JSONObject json = null;
		ArrayList<MenuItem> listCategory = new ArrayList<MenuItem>();
		JsonPaser jsonPaser = new JsonPaser();
		if( isonline == true){
			 json = jsonPaser.getJSONFromUrl(URL_GET_ALL_CATEGORY);
		}else {
			try {
				json= new JSONObject(data);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			categories = json.getJSONArray(CATEGORY);
			int size = categories.length();
			for(int i = 0;i<size;i++){
				JSONObject category = categories.getJSONObject(i);
				MenuItem menuItem = new MenuItem();
				menuItem.setId(Integer.parseInt(category.getString(ID_CATEGORY)));
				menuItem.setTitle(category.getString(TITLE));
				menuItem.setDescription(category.getString(DESCRIPTION));
				menuItem.setSlug(category.getString(SLUG));
				menuItem.setPostCount(category.getInt(POST_COUNT));
				listCategory.add(menuItem);
			}
			
		} catch (JSONException e) {
			 e.printStackTrace();
		}
		
		return listCategory;
		
		
	}
	
	public PostDetail getPost(int id){
		PostDetail post = new PostDetail();
		JsonPaser jsonPaser = new JsonPaser();
		JSONObject json = jsonPaser.getJSONFromUrl(URL_GET_DETAIL_POST+""+id);

		try {
			JSONObject jsonPost = json.getJSONObject(POST);
			post.setSubtitle(jsonPost.getString(TITLE));
			post.setDatePublish(jsonPost.getString(DATE_PUBLIC));
			post.setThumbnail(jsonPost.getString(THUMBNAIL));
			post.setContent(jsonPost.getString(CONTENT));
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return post;
		
	}
	/**
	 *  Get all post of one Category
	 * @param id
	 * @return
	 */
	public ArrayList<PostDetail> getAllPost(int id,int page){
		JSONArray posts;
		ArrayList<PostDetail> listpost = new ArrayList<PostDetail>();
		JsonPaser jsonPaser = new JsonPaser();
		
		JSONObject json = jsonPaser.getJSONFromUrl(URL_GET_ALL_POST_IN_CATEGORY+id+"&page="+page);
		//Log.i("URL", URL_GET_ALL_POST_IN_CATEGORY+id+"&page="+page);
		try {
			posts = json.getJSONArray(POSTS);
			int size = posts.length();
			//Log.i("URL", ""+size);
			for(int i = 0;i<size;i++){
				JSONObject post = posts.getJSONObject(i);
				PostDetail postDetail = new PostDetail();
				postDetail.setId(Integer.parseInt(post.getString(ID_POST)));
				postDetail.setThumbnail(post.getString(THUMBNAIL));
				postDetail.setSubtitle(post.getString(TITLE));
				//postDetail.setAuthor(post.getString(AUTHOR));
				postDetail.setDatePublish(post.getString(DATE_PUBLIC));
				//Log.i("URL", postDetail.getSubtitle());
				listpost.add(postDetail);	
			}
		} catch (JSONException e) {
			e.printStackTrace();			
		}
		
		return listpost;
	}

	/**
	 *  get all post to save database offline.
	 * @param id
	 * @param page
	 * @return
	 */
	public ArrayList<PostDetailOffline> getPostOffline (int id,int page){
		ArrayList<PostDetailOffline> listpost = new ArrayList<PostDetailOffline>();
		JSONArray posts;
		JsonPaser jsonPaser = new JsonPaser();
		JSONObject json = jsonPaser.getJSONFromUrl(URL_GET_ALL_POST_IN_CATEGORY+id+"&page="+page);
		
		try {
			posts = json.getJSONArray(POSTS);
			int size = posts.length();
			for(int i = 0;i<size;i++){
				JSONObject post = posts.getJSONObject(i);
				PostDetailOffline postdetail = new PostDetailOffline();
				postdetail.setId_server(post.getString(ID_POST));
				postdetail.setTitle(post.getString(TITLE));
				postdetail.setContent(post.getString(CONTENT));
				postdetail.setDate(post.getString(DATE_PUBLIC));
				//postdetail.setCategory(json.getString(CATEGORY_LABEL));
				JSONObject categoryObject = json.getJSONObject(CATEGORY_LABEL);
				postdetail.setCategory(categoryObject.getString(ID_CATEGORY));
				listpost.add(postdetail);
				
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return listpost;
	}
	
	public ArrayList<CommentModel> getAllComment (int id,String url ){
		Log.i("anhson", "getallcomment");
		ArrayList<CommentModel> listcomment = new ArrayList<CommentModel>();
		JSONArray comments;
		JsonPaser jsonPaser = new JsonPaser();
		JSONObject json = jsonPaser.getJSONFromUrl(url+""+id);
		try {
			JSONObject jsonPost = json.getJSONObject(POST);
			comments = jsonPost.getJSONArray(COMMENTS);
			int size = comments.length();
			Log.i("anhson", "so comment : "+size);
			for (int i = 0;i < size ;i++){
				JSONObject comment = comments.getJSONObject(i);
				CommentModel commentModel = new CommentModel();
				commentModel.setId(Integer.parseInt(comment.getString(ID_POST)));
				commentModel.setNameSubmit(comment.getString(NAME_SUBMIT_COMMENT));
				commentModel.setContentConment(comment.getString(CONTENT_COMMENT));
				commentModel.setDateConment(comment.getString(DATE_SUBMIT_COMMENT));
				listcomment.add(commentModel);	
				
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return listcomment;
		
	}
	public ArrayList<PictureModel> getAllPicture (int id,int page){
		ArrayList<PictureModel> listpictures = new ArrayList<PictureModel>();
		JSONArray pictures;
		JsonPaser jsonPaser = new JsonPaser();
		JSONObject json = jsonPaser.getJSONFromUrl(URL_GET_ALL_PICTURE+id+"&page="+page);
		try {
			pictures = json.getJSONArray(POSTS);
			int size = pictures.length();
			Log.i("anhson", ""+size);
			for(int i = 0;i<size;i++){
				JSONObject picture = pictures.getJSONObject(i);
				PictureModel pic = new PictureModel();
				pic.setId(Integer.parseInt(picture.getString(ID_POST)));
				pic.setTitle(picture.getString(TITLE));
				pic.setDate(picture.getString(DATE_PUBLIC));
				
				pic.setThumbnail(picture.getString(THUMBNAIL)); // nếu cái này không tìm thấy trong json dễ bị trả về mảng = 0 lắm.
				
				pic.setTotalComment(picture.getString(COUNT_COMMENT));
				listpictures.add(pic);
				
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		Log.i("anhson", "data : " +listpictures.size());
		return listpictures;
		
	}
	public ArrayList<PictureModel>getAllPictureOffline(String data){
		ArrayList<PictureModel> listpictures = new ArrayList<PictureModel>();
		JSONArray pictures;
		JSONObject json = null;
		try {
			json = new JSONObject(data);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			pictures = json.getJSONArray(POSTS);
			int size = pictures.length();
			Log.i("anhson", ""+size);
			for(int i = 0;i<size;i++){
				JSONObject picture = pictures.getJSONObject(i);
				PictureModel pic = new PictureModel();
				pic.setId(Integer.parseInt(picture.getString(ID_POST)));
				pic.setTitle(picture.getString(TITLE));
				pic.setDate(picture.getString(DATE_PUBLIC));
				
				pic.setThumbnail(picture.getString(THUMBNAIL)); // nếu cái này không tìm thấy trong json dễ bị trả về mảng = 0 lắm.
				
				pic.setTotalComment(picture.getString(COUNT_COMMENT));
				listpictures.add(pic);
				
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return listpictures;
	}
	/**
	 * get list picture to save offline .txt
	 * @param id
	 * @param page
	 * @return
	 */
	public String getListPictureOffline (int id, int page){
		JsonPaser jsonPaser = new  JsonPaser();
		JSONObject json = jsonPaser.getJSONFromUrl(URL_GET_ALL_PICTURE+id+"&page="+page);
		return json.toString();
	}
	public int getMaxPage (){
		int maxpage = 0;
		JsonPaser jsonPaser = new JsonPaser();
		JSONObject json = jsonPaser.getJSONFromUrl(URL_GET_ALL_PICTURE+1+"&page="+1);
		try {			
			maxpage = Integer.parseInt(json.getString(PAGES));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return maxpage;
	}
	public PictureModel getPicture (int id){
		PictureModel pic = new PictureModel();
		JsonPaser jsonPaser = new JsonPaser();
		JSONObject json = jsonPaser.getJSONFromUrl(URL_GET_DETAIL_PICTURE+""+id);
		
		try {
			JSONObject jsonPicture = json.getJSONObject(POST);
			pic.setId(Integer.parseInt(jsonPicture.getString(ID_POST)));
			pic.setTitle(jsonPicture.getString(TITLE));
			pic.setContent(jsonPicture.getString(CONTENT));
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return pic;
	}
	public int getTotalPictures (){
		int numberPic = 0;
		JsonPaser jsonPaser  = new JsonPaser();
		JSONObject json = jsonPaser.getJSONFromUrl(URL_GET_ALL_PICTURE+1+"&page="+1);
		
		try {
			JSONObject jsonNumber = json.getJSONObject(CATEGORY_LABEL);
			numberPic = Integer.parseInt(jsonNumber.getString(POST_COUNT));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return numberPic;
	}
	

}
